import { acceptHMRUpdate, defineStore } from 'pinia';

import { getDoudianCategory } from '#/api';

interface DoudianStateType {
  DoudianCategoryList: any[];
}

export const doudianStore = defineStore('doudian-store', {
  actions: {
    getDoudianCategoryFunc(id: any) {
      getDoudianCategory({ cid: id }).then((res) => {
        this.DoudianCategoryList = res;
      });
    },
    loadData(selectedOptions: any) {
      const targetOption = selectedOptions[selectedOptions.length - 1];
      targetOption.loading = true;
      getDoudianCategory({ cid: targetOption.id }).then((res) => {
        targetOption.loading = false;
        targetOption.children = res;
        this.DoudianCategoryList = [...this.DoudianCategoryList];
      });
    },
  },
  state: (): DoudianStateType => ({
    DoudianCategoryList: [],
  }),
});

// 解决热更新问题
const hot = import.meta.hot;
if (hot) {
  hot.accept(acceptHMRUpdate(doudianStore, hot));
}
